package com.meihua.shellapp.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by Administrator on 2017/5/21.
 */

class AlertController {
    private AlertDialog mAlertDialog;
    private Window mWindow;
    private DialogViewHelper mViewHelper;
    /**
     * 获取AlertDialog
     * @return
     */
    public AlertDialog getDialog() {
        return mAlertDialog;
    }

    /**
     * 获取AlertDialog的Window
     * @return
     */
    public Window getWindow() {
        return mWindow;
    }

    public AlertController(AlertDialog alertDialog, Window window) {
        this.mAlertDialog=alertDialog;
        this.mWindow=window;
    }

    public void setViewHelper(DialogViewHelper mViewHelper) {
        this.mViewHelper = mViewHelper;
    }

    /**
     * 设置文本
     *
     * @param viewid
     * @param text
     */
    public void setText(int viewid, CharSequence text) {
        mViewHelper.setText(viewid,text);
    }
    public <T extends View> T getView(int viewid) {
        return mViewHelper.getView(viewid);
    }
    /**
     * 设置点击
     *
     * @param viewid
     * @param listener
     */
    public void setOnClickListener(int viewid, View.OnClickListener listener) {
        mViewHelper.setOnClickListener(viewid,listener);
    }
    public static  class AlertParams{
        public Context mContext;
        public int mThemeResId;
        public boolean mCancelable=true;//点击空白是否可以取消
        public DialogInterface.OnCancelListener mOnCancelListener;//AlertDialog取消监听
        public DialogInterface.OnDismissListener mOnDismissListener;//AlertDialog消失监听
        public DialogInterface.OnKeyListener mOnKeyListener;//AlertDialog消失监听按键监听
        public View mView;//显示的布局
        public int mViewLayoutResId;//布局id
        //there can be gaps in the indices.  It is intended to be more memory efficient
        //* than using a HashMap to map Integers to Objects, both because it avoids
        //存放字体的修改
        public SparseArray<CharSequence> mTextArray=new SparseArray<CharSequence>();
        //存放点击事件
        public SparseArray<View.OnClickListener> mClickArray=new SparseArray<View.OnClickListener>();
        //默认宽度
        public int mWith= ViewGroup.LayoutParams.WRAP_CONTENT;
        //动画
        public int mAnimation=0;
        //位置默认居中
        public int mGravity= Gravity.CENTER;
        //默认高度
        public int mHeight=ViewGroup.LayoutParams.WRAP_CONTENT;

        public AlertParams(Context context, int themeResId) {
            this.mContext=context;
            this.mThemeResId=themeResId;
        }

        /**
         * 绑定和设置参数
         * @param mAlert
         */
        public void apply(AlertController mAlert) {
            //设置参数
            //1设置布局
            DialogViewHelper viewHelper=null;
            if(mViewLayoutResId!=0){
                viewHelper=new DialogViewHelper(mContext,mViewLayoutResId);
            }
            if(mView!=null){
                viewHelper=new DialogViewHelper();
                viewHelper.setContentView(mView);
            }
            if(viewHelper==null){
                throw new IllegalArgumentException("请设置布局setContentView");
            }
            //给dialog设置布局
            mAlert.getDialog().setContentView(viewHelper.getContentView());

            //设置AlertController辅助类
            mAlert.setViewHelper(viewHelper);

            //2设置文本
            int mTextSize = mTextArray.size();
            for(int i=0;i<mTextSize;i++){
                mAlert.setText(mTextArray.keyAt(i),mTextArray.valueAt(i));
            }
            //3设置点击
            int mClickSize = mClickArray.size();
            for(int i=0;i<mClickSize;i++){
                mAlert.setOnClickListener(mClickArray.keyAt(i),mClickArray.valueAt(i));
            }

            //4设置自定效果  全屏   从底部弹出   默认动画
            Window window = mAlert.getWindow();
            //设置位置
            window.setGravity(mGravity);
            //设置动画
            if(mAnimation!=0){
                window.setWindowAnimations(mAnimation);
            }
            //设置宽高
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width=mWith;
            attributes.height=mHeight;
            window.setAttributes(attributes);

        }
    }
}
